home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13127 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.1 KB  |  109 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Does C convert float to double internally ?
  5. Date: Thu, 04 Apr 96 17:57:17 GMT
  6. Organization: none
  7. Message-ID: <828640637snz@genesis.demon.co.uk>
  8. References: <4jsllh$hkf@harbinger.cc.monash.edu.au> <4jv2ho$r1o@harbinger.cc.monash.edu.au>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jv2ho$r1o@harbinger.cc.monash.edu.au>
  15.            bcheung@yoyo.cc.monash.edu.au "Biggles Cheung" writes:
  16.  
  17. >Biggles Cheung (bcheung@yoyo.cc.monash.edu.au) wrote:
  18. >: I was told that C compiler automatically converts "float" variable
  19. >: internally to "double" for + - * / , etc operations. Is this true?
  20. >
  21. >After I have read some books on C/C++, they all say that K&R C used to
  22. >do _implicit_ conversion from float to double both in unary and binary
  23. >operations, while ANSI C has dropped the implicit conversion and happily
  24. >allows float to be float at all time.
  25.  
  26. ANSI allows either approach, at least in effect.
  27.  
  28. 6.1.2.5
  29.  
  30. "The values of floating point operands and the results of floating point
  31.  expressions may be represented in greater precision and range than that
  32.  required by the type; the types are not changed thereby"
  33.  
  34. In fact the calculation can be performed in any representation greater
  35. than the one in question. In this case, float, it could be double,
  36. long double, or a representation that doesn't correspond to any C type.
  37.  
  38. What you are guaranteed is:
  39.  
  40. 6.2.1.4
  41.  
  42. "When a float is promoted to double or long double, or a double is promoted
  43.  to long double, its value is unchanged"
  44.  
  45. i.e. the conversion doesn't introduce inaccuracies. I guess that doesn't
  46. necessarily apply in this case since strictly no type conversion is happening,
  47. just a change of representation.
  48.  
  49. Assignment and casting are guaranteed to convert to the correct representation
  50. for the target type (or at least act as if they have).
  51.  
  52. >Can I safely assume now that ALL current C/C++ compliers are following ANSI C
  53. >standard?
  54. >
  55. >: 
  56. >: Can I force the C compiler not to do the conversion as I have an
  57. >: assignment on finding out various computing errors of different
  58. >: precisions? 
  59. >:
  60. >
  61. >I have read also the Turbo C++ manual and it says "tcc -ff- " command
  62. >line option can force the command line compiler to follow STRICT ANSI C
  63. >rule on conversion, i.e. no implicit conversion.
  64. >
  65. >Can I now be sure that if I use "tcc -ff- program.c", there is _no_
  66. >implicit conversion? But what about the IDE interface? 
  67.  
  68. Often the floating point registers on a system hold more precision for
  69. accuracy considerations. You probably can't stop them using it. However the
  70. result must still be a float as far as the type system is concerned.
  71.  
  72. >Can someone suggest a simple program to test if there is any implicit
  73. >conversion happening behind the curtain?
  74.  
  75. #include <stdio.h>
  76.  
  77. ...
  78.  
  79.     float a, b;
  80.  
  81.     printf("%lu\n", (unsigned long)sizeof(a+b));
  82.  
  83. If float and double have different representations on your system then
  84. the chances are that they will have different sizes. For a conforming
  85. implementation this must print out the same value as sizeof(float). There's
  86. nop guarantee that the expression a+b won't use more bits of precision
  87. internally though.
  88.  
  89. >If I am to find the single precision value of say, 12.3457 to the power
  90. >of 13 by multiplying 12.3456 thirteen times, does that mean I have to
  91. >store the result of every _single_ multiplication to a temporary variable
  92. >of float to enforce the single precision? 
  93.  
  94. You could cast the result of each operator in the expression. This is an
  95. area where I wouldn't trust the compiler much though
  96.  
  97. >What if I have a very complicated mathematical expression like Taylor's
  98. >series plus sqrt plus more ... ? How many temporary variables I need and
  99. >when is the best time or where is the best spot to insert such
  100. >_nuisance_?
  101.  
  102. Typically the extra precision is an advantage, not a disadvantage.
  103.  
  104. -- 
  105. -----------------------------------------
  106. Lawrence Kirby | fred@genesis.demon.co.uk
  107. Wilts, England | 70734.126@compuserve.com
  108. -----------------------------------------
  109.